package com.gw.vip.server.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gw.vip.server.bo.TransAccSumBo;
import com.gw.vip.server.po.TmpTrans;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * @author GW
 * @description
 * @since 2022-05-07
 */
public interface TmpTransMapper extends BaseMapper<TmpTrans> {
    /**
     * 清空表
     */
    @Update("TRUNCATE TABLE tmp_trans")
    void truncate();

    /**
     * 初始化数据
     * @param clearDate
     * @return
     */
    @Insert("INSERT INTO tmp_trans SELECT * FROM trans WHERE clear_date=#{clearDate}")
    Integer initData(LocalDate clearDate);

    /**
     * 初始化数据
     * @param clearDate
     */
    @Insert("INSERT INTO tmp_trans SELECT * FROM his_trans WHERE clear_date=#{clearDate}")
    void initDataFromHis(LocalDate clearDate);

    /**
     * 统计帐户在清算日期内的金额变动
     * @param accountId
     * @param clearDate
     * @return
     */
    @Select("SELECT SUM(txn_amt*balance_dir) sum_amt FROM (SELECT * FROM tmp_trans WHERE account=#{accountId} AND clear_date=#{clearDate} AND status='S') trans LEFT JOIN sys_order_type ON trans.order_type = sys_order_type.id")
    BigDecimal getDiffAmtByAccount(String accountId, LocalDate clearDate);

    /**
     * 清算日期内的交易按照资金类型汇总
     * @param clearDate
     * @return
     */
    @Select("SELECT capital_id, SUM(txn_amt*balance_dir) sum_amt FROM tmp_trans LEFT JOIN sys_order_type ON tmp_trans.order_type = sys_order_type.id WHERE tmp_trans.status='S' AND clear_date=#{clearDate} GROUP BY capital_id")
    List<TransAccSumBo> transSum(LocalDate clearDate);

    /**
     * 根据清算日期查询
     * @param clearDate
     * @return
     */
    @Select("SELECT * FROM tmp_trans WHERE clear_date=#{clearDate}")
    List<TmpTrans> listByClearDate(LocalDate clearDate);
}
